<template>
  <div class="mod-config" >
    <div v-show="isShow">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.keywords" placeholder="活动标题" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button v-if="isAuth('spike:activity:add')" type="primary" @click="addOrUpdateHandle()">新增活动</el-button>
          <el-button v-if="isAuth('spike:activity:remove')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column
          prop="activityId"
          header-align="center"
          align="center"
          label="活动ID">
        </el-table-column>
        <el-table-column
          prop="activityTitle"
          header-align="center"
          align="center"
          label="活动标题">
        </el-table-column>
        <el-table-column
          prop="beginTime"
          header-align="center"
          align="center"
          label="活动开始时间">
        </el-table-column>
        <el-table-column
          prop="endTime"
          header-align="center"
          align="center"
          label="活动结束时间">
        </el-table-column>
        <el-table-column
          prop="publishRemark"
          header-align="center"
          align="center"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          label="活动状态">
          <template slot-scope="scope">
            <el-switch
              @change="_updateState(scope.row.activityId,scope.row.state,scope.row.endTime)"
              v-model="scope.row.state === 1"
              active-text="线上"
              inactive-text="下线">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <!-- <el-button type="text" size="small" @click="forward(scope.row.activityId,scope.row.state)">设置商品</el-button> -->
            <!-- <el-button type="text" size="small" v-if="isAuth('spike:activity:modify')" @click="addActivity(scope.row.activityId, 1)">设置时段</el-button> -->
            <el-button type="text" size="small" v-if="isAuth('spike:activity:modify')" @click="addOrUpdateHandle(scope.row.activityId)">修改</el-button>
            <el-button type="text" size="small" v-if="isAuth('spike:activity:modify')" @click="addActivityGoodss(scope.row.activityId, 1)">设置商品</el-button>
            <el-button type="text" size="small" v-if="isAuth('spike:activity:remove')" @click="deleteHandle(scope.row.activityId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!-- <add-activity-goods v-if="addOrUpdateVisible" ref="addActivityGoods" @refreshDataList="getDataList"></add-activity-goods> -->
    </div>
    <div v-show="!isShow" >
      <add-Activity-Goods ref="AddActivityGoods"></add-Activity-Goods>
      <el-button @click="isShow=true" type="primary" round icon="el-icon-arrow-left">返回活动管理</el-button>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="updateState"
      width="30%">
      <span style="color: #a1a2a6;font-size: 8px;"><i class="el-icon-warning"></i>该活动已结束，如需上线请更新活动时间</span>
      <br>
      <el-date-picker
        v-model="updateTime"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format = "yyyy-MM-dd HH:mm:ss">
      </el-date-picker>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateState = false">取 消</el-button>
        <el-button type="primary" @click="updateStateAjax()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from './add-activity'
  // import Goods from './goods'
  // import AddActivity from './whole-point-activities'
  // import AddActivity from './aa'
  import AddActivityGoods from './bb'
  export default {
    data () {
      return {
        updateTime: new Date(),
        updateState: false,
        updateStateData: {
          id: Number(),
          state: true
        },
        isShow: true,
        // isShow1: true,
        // isShow2: true,
        dataForm: {
          keywords: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      // AddActivity,
      AddActivityGoods
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/activity/spike/queryActivityList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.isShow = true
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // addActivity (id) {
      //   this.addOrUpdateVisible = true
      //   this.$nextTick(() => {
      //     this.$refs.addActivity.init(id)
      //   })
      // },
      // isShowTime () {
      //   this.isShow1 = true
      //   this.isShow = true
      // },
      // isShowTime2 () {
      //   this.isShow2 = true
      //   this.isShow = true
      // },
      addActivityGoodss (id, state) {
        localStorage.setItem('spikeActivityId', id)
        localStorage.setItem('activityState', state)
        this.isShow = false
        this.$nextTick(() => {
          this.$refs.AddActivityGoods.getGoodsDataLists(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.activityId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/activity/spike/remove'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      updateStateAjax () {
        this.updateState = false
        this.$http({
          url: this.$http.adornUrl('/activity/spike/modify'),
          method: 'post',
          data: this.$http.adornData({
            'activityId': this.updateStateData.id,
            'state': this.updateStateData.state ? 0 : 1,
            'beginTime': this.updateTime[0],
            'endTime': this.updateTime[1]
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      _updateState (id, state, endTime) {
        let that = this
        this.$confirm(`确定更改状态吗`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.updateStateData.id = id
          that.updateStateData.state = state
          if (state === 0) {
            if (new Date(endTime) - new Date() <= 0) {
              that.updateState = true
              return
            }
          }
          this.updateStateAjax()
        }).catch(() => {
          this.getDataList()
        })
      }
      // ,
      // // 转发
      // addActivity (id, state) {
      //   localStorage.setItem('spikeActivityId', id)
      //   localStorage.setItem('activityState', state)
      //   this.isShow1 = false
      //   this.isShow = false
      //   this.$nextTick(() => {
      //     this.$refs.AddActivity.getGoodsDataLists(id)
      //   })
      // }
    }
  }
</script>
<style>

  .el-row {
    margin-bottom: 20px;
    &:last-child {
       margin-bottom: 0;
     }
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
